/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*
 *
 * This file is part of the TeX Font Tools package.
 *
 * Copyright  1997 Jakob Stoklund Olesen.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

static char     rcsid[] =
"$Id: c.Error 3.2 1997/11/02 23:18:20 stoklund Stable $";

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*
 * $Revision: 3.2 $	$Date: 1997/11/02 23:18:20 $
 *
 * Error reporting and memory checks
 *
 * $Log: c.Error $
 * Revision 3.2  1997/11/02 23:18:20  stoklund
 * Errors made GNU compliant
 * (Error_Filename) New function
 *
 * Revision 3.1  1997/11/01 12:47:32  stoklund
 * *** empty log message ***
 *
 * Revision 2.1  1997/07/04 20:43:34  stoklund
 * Copyright fix, Log cleanup
 *
 *- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "Error.h"

/* All error messages are prefixed with the Program name */
static const char *progname = "";

/* If there is a current input file, print it too. */
static const char *filename = NULL;

/* Initialise the error system, setting the prog name */
void
Error_Initialise(const char *name)
{
  progname = name;
}

/* Set the current filename, NULL for none. */
void
Error_Filename(const char *name)
{
  filename = name;
}

/* Report a warning, calling syntax is like printf */
void
Error_Warning(const char *format,...)
{
  va_list         ap;

  va_start(ap, format);
  fprintf(stderr, "%s warning: ", progname);
  if(filename)
    fprintf(stderr, "%s: ", filename);
  vfprintf(stderr, format, ap);
  fputs("\n", stderr);
  va_end(ap);
}

/* Report an error, calling syntax is like printf */
void
Error_Error(const char *format,...)
{
  va_list         ap;

  va_start(ap, format);
  fprintf(stderr, "%s error: ", progname);
  if(filename)
    fprintf(stderr, "%s: ", filename);
  vfprintf(stderr, format, ap);
  fputs("\n", stderr);
  va_end(ap);
}

/*
 * Report a fatal error, calling syntax is like printf. Program will
 * terminate
 */
void
Error_Fatal(const char *format,...)
{
  va_list         ap;

  va_start(ap, format);
  fprintf(stderr, "%s fatal error: ", progname);
  if(filename)
    fprintf(stderr, "%s: ", filename);
  vfprintf(stderr, format, ap);
  fputs("\n", stderr);
  va_end(ap);
  exit(EXIT_FAILURE);
}

/*
 * Check a memory pointer. If it is NULL exit with a fatal error otherwise
 * the pointer is returned.
 */
void           *
Error_Memory(void *ptr, const char *where)
{
  if (ptr)
    return ptr;
  if (where)
    Error_Fatal("out of memory (%s)", where);
  else
    Error_Fatal("out of memory");
  return NULL;			/* to keep the compiler quiet */
}
